package com.webware.one.client.popup;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;

public abstract class AbstractButtonsPanel extends HorizontalPanel {

  public Map<EButton, Button> buttons = new HashMap<EButton, Button>();

  public AbstractButtonsPanel(List<EButton> buttonsDescription) {
    setWidth("100%");
    setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    setStyleName("dlgButtons");
    for (final EButton choice : buttonsDescription) {
      Button button = new Button(choice.getText());
      button.setStyleName("dlgButton");
      button.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
          onButtonClick(choice);
        }
      });
      add(button);
      this.buttons.put(choice, button);
    }

  }

  public AbstractButtonsPanel(EButton... buttonsDescription) {
    this(Arrays.asList(buttonsDescription));
  }

  public void setButtonsEnabled(boolean enabled) {
    for (Button button : this.buttons.values()) {
      button.setEnabled(enabled);
    }
  }

  public void setButtonEnabled(boolean enabled, EButton buttonEnum) {
    Button exceptButton = this.buttons.get(buttonEnum);
    if (exceptButton != null) exceptButton.setEnabled(enabled);
  }

  public void setFocuse(EButton choice) {
    Button button = this.buttons.get(choice);
    if (button != null) button.setFocus(true);
  }

  public abstract void onButtonClick(EButton choice);

}
